package org.fuys.coder.domain.auth.service.vertify.token.impl;

import org.fuys.coder.common.constants.RedisConstants;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.holder.TokenExpireHolder;
import org.fuys.coder.common.util.DateUtil;
import org.fuys.coder.domain.auth.repository.ITokenRepository;
import org.fuys.coder.infrastructure.util.RedisUtil;
import org.fuys.coder.domain.auth.service.vertify.token.ITrustTokenService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.auth.service.vertify.token.impl
 * @className: TrustTokenServiceImpl
 * @author: WangJing
 * @description: 信任token实现类
 * @date: 2024/8/11 11:16
 * @version: 1.0
 */

@Service
public class TrustTokenServiceImpl implements ITrustTokenService {

    @Resource
    private ITokenRepository tokenRepository;

    //获得操作token
    @Override
    public Pair<String[],Long[]> operationToken(Integer userId, Long substanceId, String... types) {
        String redisPrefix= RedisConstants.REDIS_FIELD_USER+RedisConstants.REDIS_USE_TOKEN+RedisConstants.REDIS_SPLIT;
        //如果非空 则添加分层内容的token
        if(!ObjectUtils.isEmpty(substanceId)){
            redisPrefix+=substanceId+RedisConstants.REDIS_SPLIT;
        }
        String[] result=new String[types.length];
        Long[] expires=new Long[types.length];
        for (int i = 0; i < types.length; i++) {
            String redisKey=redisPrefix+types[i]+RedisConstants.REDIS_SPLIT+userId;
            final Object trustToken = tokenRepository.getToken(redisKey);
            if(!ObjectUtils.isEmpty(trustToken)){
                result[i]=trustToken.toString();
                expires[i]= TokenExpireHolder.expireMap.get(types[i]);
                continue;
            }
            final String token = UUID.randomUUID().toString();
            tokenRepository.setToken(redisKey,token,3, TimeUnit.HOURS);
            result[i]=token;
            expires[i]= DateUtil.addTimeFromCur(
                    TokenExpireHolder.expireMap.get(types[i]).intValue(),
                    TimeUnit.HOURS).getTime();
        }
        return new Pair<String[],Long[]>(result,expires);
    }

    @Override
    public boolean checkToken(Integer userId, Long substanceId, String type,String token) {
        String redisPrefix= RedisConstants.REDIS_FIELD_USER+RedisConstants.REDIS_USE_TOKEN+RedisConstants.REDIS_SPLIT;
        //如果非空 则添加分层内容的token
        if(!ObjectUtils.isEmpty(substanceId)){
            redisPrefix+=substanceId+RedisConstants.REDIS_SPLIT;
        }
        redisPrefix+=type+RedisConstants.REDIS_SPLIT+userId;
        final Object trustToken = tokenRepository.getToken(redisPrefix);
        if(ObjectUtils.isEmpty(trustToken)){
            return false;
        }
        tokenRepository.deleteToken(redisPrefix);
        return trustToken.toString().equals(token);
    }
}
